package com.irdstudio.apicenter.gateway.comm;

import java.util.Hashtable;
import java.util.Map;

import com.irdstudio.apicenter.gateway.core.dm.AppChannelForward;
import com.irdstudio.apicenter.gateway.util.spring.BeanUtils;

/**
 * MACCS渠道工厂（根据不同的渠道类型创建不同的渠道连接)
 * 
 * @author guangming.li
 * @version 1.0
 * @date 2015-05-22
 */
public class MaccsChannelFactory {

	/* 渠道定义信息 */
	private static Map<String, AppChannelForward> channelTable = null;

	/**
	 * 向渠道工厂中增加渠道定义信息
	 * 
	 * @param acf
	 */
	public static void addChannelDefine(AppChannelForward acf) {
		if (channelTable == null)
			channelTable = new Hashtable<String, AppChannelForward>();
		channelTable.put(acf.getForwardChannelId(), acf);
	}

	/**
	 * 根据渠道ID得到渠道连接对象
	 * 
	 * @param channelId
	 * @return
	 */
	public static MaccsChannelClient getChannelClient(String channelId) {
		AppChannelForward acf = channelTable.get(channelId);
		if (acf != null) {
			MaccsChannelClient mcc = (MaccsChannelClient) BeanUtils.getBean(acf
					.getForwardChannelType()); // 通过数据库配置的beanId
			mcc.setChannelDefine(acf);
			return mcc;
		} else {
			return null;
		}
	}
}
